package com.example.shop_front_api.controller;

import com.example.shop_front_api.entity.Goods;
import com.example.shop_front_api.entity.News;
import com.example.shop_front_api.service.GoodsService;
import com.example.shop_front_api.service.NewsService;
import com.example.shop_front_api.utils.JsonResult;
import com.example.shop_front_api.utils.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("api/data")
public class DataController {

    @Autowired
    private NewsService newsService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ElasticsearchOperations elasticsearchOperations;

    // 资讯相关接口
    @PostMapping("addNews")
    public JsonResult addNews(@RequestBody News news) {
        news.setCreateTime(Tool.getCurrentTime());
        int result = newsService.addNews(news);
        if (result == 0) {
            return JsonResult.failed("添加失败");
        }
        elasticsearchOperations.save(news);
        return JsonResult.success("添加成功", news);
    }

    @GetMapping("getNews")
    public JsonResult getNews(@RequestParam("id") Long id) {
        News news = newsService.getNewsById(id);
        if (news == null) {
            return JsonResult.failed("资讯不存在");
        }
        return JsonResult.success("查询成功", news);
    }

    @GetMapping("listNews")
    public JsonResult listNews() {
        List<News> newsList = newsService.getAllNews();
        return JsonResult.success("查询成功", newsList);
    }

    @GetMapping("deleteNews")
    public JsonResult deleteNews(@RequestParam("id") Long id) {
        int result = newsService.deleteNewsById(id);
        if (result == 0) {
            return JsonResult.failed("删除失败或资讯不存在");
        }
        elasticsearchOperations.delete(id.toString(), News.class);
        return JsonResult.success("删除成功", null);
    }

    // 商品相关接口
    @PostMapping("addGoods")
    public JsonResult addGoods(@RequestBody Goods goods) {
        int result = goodsService.addGoods(goods);
        if (result == 0) {
            return JsonResult.failed("添加失败");
        }
        elasticsearchOperations.save(goods);
        return JsonResult.success("添加成功", null);
    }

    @GetMapping("listGoods")
    public JsonResult listGoods(@RequestParam("cate_id") int cateId,
                                @RequestParam("level") int level) {
        List<Goods> goodsList = goodsService.getGoodsByCateId(cateId, level);
        return JsonResult.success("获取成功", goodsList);
    }

    @GetMapping("getGoods")
    public JsonResult getGoods(@RequestParam("id") Long id) {
        Goods goods = goodsService.getGoodsById(id);
        if (goods == null) {
            return JsonResult.failed("商品不存在");
        }
        return JsonResult.success("查询成功", goods);
    }

    @GetMapping("deleteGoods")
    public JsonResult deleteGoods(@RequestParam("id") Long id) {
        int result = goodsService.deleteGoodsById(id);
        if (result == 0) {
            return JsonResult.failed("删除失败或商品不存在");
        }
        elasticsearchOperations.delete(id.toString(), Goods.class);
        return JsonResult.success("删除成功", null);
    }
}